package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.InventoryDull;
import com.sl.au.entity.InventoryReduce;
import com.sl.au.entity.QuoteDevice;
import com.sl.au.entity.User;
import com.sl.au.service.InventoryDullService;
import com.sl.au.service.InventoryReduceService;

@RestController
@RequestMapping(value="/ui/inventoryReduce")
public class InventoryReduceController extends BaseController {

	@Autowired
	InventoryReduceService inventoryReduceService;
	@Autowired
	InventoryDullService inventoryDullService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(){
		ModelAndView modelAndView = createLayoutView("inventoryReduce/inventoryReduceindex");
		Account a = this.getMyAccount();
		User user = a.getUser();
		String role = a.getRole().getName();
		modelAndView.addObject("user", user);
		modelAndView.addObject("role",role);
	return modelAndView;
	}
	//获得占库记录的列表
	@RequestMapping(value="/getpage",method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		long count=0;
		String key=request.getParameter("key");
		List<InventoryReduce> inventoryReduces = inventoryReduceService.getInventoryReduce(start, length, key, null, null);
		if(key==null){
			count=inventoryReduceService.getRepository().count();
		}
		if(key!=null){
		count=inventoryReduceService.getRepository().findCount(key);
		}
		map.put("data", inventoryReduces);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	//获得价格单中已替换设备
	@RequestMapping(value="/getIsReplacePage",method = RequestMethod.GET)
	public Map<String, Object> getIsReplacePage(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String quoteDeviceId=request.getParameter("quoteDeviceId");
		Long count=null;
		List<InventoryReduce> inventoryReduce = inventoryReduceService.getQuoteIsReplace(start, length, quoteDeviceId,null, null);
		count=inventoryReduceService.getRepository().findIsReplaceCount(quoteDeviceId);
		map.put("data", inventoryReduce);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	//保存降库设备
			@RequestMapping(value = "/save", method = RequestMethod.POST)
			public Map<String, Object> saveForeignTradeQuote(HttpServletRequest request, 
					@RequestBody InventoryReduce inventoryReduce) {
				Map<String, Object> map = new HashMap<String, Object>();
				inventoryReduceService.getRepository().save(inventoryReduce);
				map.put("code", 0);
				return map;
			}
			
	//强制释放占库
			@RequestMapping(value = "/release", method = RequestMethod.GET)
			public Map<String, Object> delMainDevice(HttpServletRequest request){
				Map<String, Object> map = new HashMap<String, Object>();
			    String ids=request.getParameter("ids");
			    String id[]=ids.split(",");
			    List<InventoryReduce> inventoryReduces=new ArrayList<InventoryReduce>();
			    for(int i=0;i<id.length;i++){
			    	String inventoryReduceId = id[i];
			    	InventoryReduce inventoryReduce = inventoryReduceService.getRepository().findOne(inventoryReduceId);
			    	inventoryReduces.add(inventoryReduce);
			    	int number = inventoryReduce.getNumber();
			    	String inventoryDullId = inventoryReduce.getInventoryDullId();
					InventoryDull inventoryDull = inventoryDullService.getRepository().findOne(inventoryDullId);
					int usableNum = inventoryDull.getUsableNum();
					inventoryDull.setUsableNum(usableNum+number);
					inventoryDullService.getRepository().save(inventoryDull);
					QuoteDevice quoteDevice = inventoryReduce.getQuoteDevice();
					quoteDevice.setInventoryState("0");
					inventoryReduceService.getDeviceRepository().save(quoteDevice);
			    }
			    inventoryReduceService.getRepository().deleteInBatch(inventoryReduces);
				map.put("code", 0);
				return map;	
			
			
			
			}
}
